package com.zitangkou.user.domain.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zitangkou.user.domain.IPermissionDomainService;
import com.zitangkou.user.domain.PermissionDomain;
import com.zitangkou.user.domain.converter.IPermissionDomainConverter;
import com.zitangkou.user.entity.PermissionDo;
import com.zitangkou.common.enums.StatusEnum;
import com.zitangkou.user.mapper.PermissionMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Deng Ningning
 * @version 1.0
 * @date 2020/11/13 21:59
 */
@Service
public class PermissionDomainServiceImpl implements IPermissionDomainService {

    @Resource
    private PermissionMapper permissionMapper;

    @Resource
    private IPermissionDomainConverter permissionDomainConverter;

    @Override
    public IPage<PermissionDomain> selectPageByConditions(Integer current, Integer size, String keyword, StatusEnum status) {
        IPage<PermissionDo> page = new Page<>(current, size);
        page = permissionMapper.selectPageByConditions(page, StringUtils.trimToEmpty(keyword), status);
        return new Page<PermissionDomain>()
                .setRecords(permissionDomainConverter.toDomainList(page.getRecords()))
                .setTotal(page.getTotal());
    }

    @Override
    public List<PermissionDomain> selectPermissionsByIdList(List<String> permissionIdList) {
        if (CollectionUtils.isEmpty(permissionIdList)) {
            return new ArrayList<>();
        }
        return permissionDomainConverter.toDomainList(
                permissionMapper.selectList(new LambdaQueryWrapper<PermissionDo>().eq(PermissionDo::getStatus, StatusEnum.ENABLED)
                        .in(PermissionDo::getPermissionId, permissionIdList)));
    }

    @Override
    public boolean insertPermission(PermissionDomain permissionDomain) {
        return permissionMapper.insert(permissionDomainConverter.toDo(permissionDomain)) > 0;
    }

    @Override
    public boolean updatePermission(PermissionDomain permissionDomain) {
        return permissionMapper.updateById(permissionDomainConverter.toDo(permissionDomain)) > 0;
    }

    @Override
    public boolean updatePermissionStatus(PermissionDomain permissionDomain) {
        return permissionMapper.updateById(permissionDomainConverter.toDo(permissionDomain)) > 0;
    }

    @Override
    public PermissionDomain getPermissionById(String permissionId) {
        return permissionDomainConverter.toDomain(permissionMapper.selectById(permissionId));
    }

    @Override
    public int countByConditions(PermissionDomain permissionDomain) {
        return permissionMapper.selectCount(new QueryWrapper<PermissionDo>()
                .lambda()
                .eq(PermissionDo::getEnterpriseId, permissionDomain)
                .eq(StringUtils.isNotBlank(permissionDomain.getPermissionName()), PermissionDo::getPermissionName,
                        permissionDomain.getPermissionName())
                .eq(StringUtils.isNotBlank(permissionDomain.getPermissionValue()), PermissionDo::getPermissionName,
                        permissionDomain.getPermissionValue())
                .eq(PermissionDo::getStatus, StatusEnum.ENABLED)
                .ne(StringUtils.isNotBlank(permissionDomain.getPermissionId()), PermissionDo::getPermissionId,
                        permissionDomain.getPermissionId()));
    }
}
